import { menu } from "../../api";
import { storage } from "../../libs";
const debug = process.env.NODE_ENV !== "production";

const MUTATION_TYPES = {
  MENU: "MENU",
  MENU_LIST: "MENU_LIST",
  SUBMIT: "SUBMIT",
  MENU_CHILD_NODE: "MENU_CHILD_NODE",
  IS_MENU_CHILD_NODE: "IS_MENU_CHILD_NODE",
  ORDER_TABLE_TOTAL: "ORDER_TABLE_TOTAL"
};

function readStorage () {
  return storage.read("MENU_LIST");
}

function writeStorage (menuList) {
  storage.write("MENU_LIST", menuList);
}

function writeStorageMenuChild (menuChildNode) {
  storage.write("MENU_CHILD_NODE", menuChildNode);
}
function readStorageMenuChild () {
  return storage.read("MENU_CHILD_NODE");
}

const state = {
  menu: {},
  menuList: [],
  menuChildNode: [],
  isMenuChildNode: true
};
const getters = {
  resource (state, getters, rootState, rootGetters) {
    console.log("", state, getters, rootState, rootGetters);
    return (scope) =>
      menu.resources?.filter((resource) => resource.scope === scope)?.[0];
  }
};

const mutations = {
  [MUTATION_TYPES.MENU] (state, menu) {
    state.menu = menu;
  },
  [MUTATION_TYPES.MENU_LIST] (state, { menuList }) {
    state.menuList = menuList;
    writeStorage(menuList);
  },
  [MUTATION_TYPES.SUBMIT] (state, menu) {
    console.log("SUBMIT:", state, menu);
  },
  [MUTATION_TYPES.MENU_CHILD_NODE] (state, menuChildNode) {
    state.menuChildNode = menuChildNode;
    writeStorageMenuChild(menuChildNode);
  },
  [MUTATION_TYPES.IS_MENU_CHILD_NODE] (state, isMenuChildNode) {
    state.isMenuChildNode = isMenuChildNode;
  },
  [MUTATION_TYPES.ORDER_TABLE_TOTAL] (state, orderTableTotal = {}) {
    state.orderTableTotal = orderTableTotal || {};
  }
};

const actions = {
  async load ({ commit }, menuId) {
    if (!menuId) {
      console.log("菜单ID无效", menuId);
      return;
    }
    let response = await menu.load(menuId);
    if (response.code === 0) {

      commit(MUTATION_TYPES.MENU, response.data);
      let data = response.data;
      // hierarchy {1级菜单} {2级菜单} {3级菜单}
      if (data.children || data.hierarchy === 3) {
        let menuChild = readStorageMenuChild();
        commit(MUTATION_TYPES.MENU_CHILD_NODE, data.children || menuChild);
        commit(MUTATION_TYPES.IS_MENU_CHILD_NODE, true);
      } else {
        // let menuChild = readStorageMenuChild()
        // console.log('menuChild....', menuChild)
        // commit(MUTATION_TYPES.MENU_CHILD_NODE, menuChild)
        commit(MUTATION_TYPES.IS_MENU_CHILD_NODE, false);
      }

      return response.data;
    }
  },
  async loadAll ({ commit }, { force = true } = {}) {
    if (!force) {
      let menuList = readStorage();
      if (menuList?.length > 0) {
        commit(MUTATION_TYPES.MENU_LIST, { menuList });
        return menuList;
      }
    }

    let filter = { parentId: [0], size: 500 };
    let response = await menu.tree(filter);
    if (response.code === 0) {
      let { records: menuList } = response.data;
      commit(MUTATION_TYPES.MENU_LIST, { menuList });
      return menuList;
    }
  },
  async submit ({ commit }, menu) {
    let response = menu.submit(menu);
    if (response.code === 0) {
      commit(MUTATION_TYPES.SUBMIT, response.data);
    }
    return response;
  },
  async setMenu ({ commit }, { menu }) {
    commit(MUTATION_TYPES.MENU, menu);
  }
};

export default {
  strict: debug,
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
